# -*- coding: utf-8 -*-

from odoo import models, fields, api
import datetime


class wliotDeviceCommLog(models.Model):
    _name = 'wliot.device.command.log'

    device = fields.Many2one('wliot.device', string=u'设备')
    partner_id = fields.Many2one('res.partner', string=u'操作人')
    barcode = fields.Char(string=u'阀门追溯号')
    IMEI = fields.Char(string=u'IMEI')
    order_type = fields.Selection([
        ('setting', u'设置'),
        ('control', u'控制')
    ], string=u'指令类型')
    code = fields.Integer(string=u'命令码')
    command_id = fields.Char(string=u'电信指令字')
    code_id = fields.Char(string=u'指令字')
    send_code = fields.Boolean(string=u'发送指令')
    response_code = fields.Boolean(string=u'回复指令')
    status = fields.Selection([
        ('send', u'发送'),
        ('work', u'生效'),
        ('fail', u'失败')
    ], string=u'状态')
    self_open_degree = fields.Integer(string=u'手动开合度')
    auto_open_degree_per20 = fields.Integer(string=u'自动20%开合度')
    auto_open_degree_per40 = fields.Integer(string=u'自动40%开合度')
    auto_open_degree_per60 = fields.Integer(string=u'自动60%开合度')
    auto_open_degree_per80 = fields.Integer(string=u'自动80%开合度')
    auto_open_degree_per100 = fields.Integer(string=u'自动100%开合度')
    calibrate_sensor = fields.Float(string=u'设备校准值', digits=(10, 1))
    frequence = fields.Integer(string=u'检测频率')
    alarm = fields.Float(string=u'报警值')
    calibrate_temp = fields.Float(string=u'温度校准值', digits=(10, 1))
    calibrate_time = fields.Datetime(string=u'校准时间值')
    back_value = fields.Char(string=u'返回值')
    code_time = fields.Char(string=u'命令时间')

    @api.multi
    def get_value(self):
        ''' 获取值 '''
        self.ensure_one()
        device = self.env['wliot.device'].sudo().search([('id', '=', self.device.id)])
        partner = self.env['res.partner'].sudo().search([('id', '=', self.partner_id.id)])
        if self.status == 'send':
            s = u'等待执行'
        elif self.status == 'work':
            s = u'执行成功'
        elif self.status == 'fail':
            s = u'执行失败'
        value = {
            'device': device.barcode,
            'partner': partner.name,
            'barcode': self.barcode,
            'IMEI': self.IMEI,
            'code': self.code,
            'command_id': self.command_id,
            'send_code': self.send_code,
            'response_code': self.response_code,
            'status': s,
            'self_open_degree': self.self_open_degree,
            # 'calibrate_time': str(datetime.datetime.strptime(self.calibrate_time,
            #                                                  '%Y-%m-%d %H:%M:%S') + datetime.timedelta(
            #     hours=8)),
            'frequence': self.frequence,
            'alarm': self.alarm,
            'calibrate_temp': self.calibrate_temp,
            'auto_open_degree_per20': self.auto_open_degree_per20,
            'auto_open_degree_per40': self.auto_open_degree_per40,
            'auto_open_degree_per60': self.auto_open_degree_per60,
            'auto_open_degree_per80': self.auto_open_degree_per80,
            'auto_open_degree_per100': self.auto_open_degree_per100,
            'back_value': self.back_value,
            'code_time': str(datetime.datetime.strptime(self.code_time,
                                                        '%Y-%m-%d %H:%M:%S') + datetime.timedelta(
                hours=8)),
        }
        return value
